package com.inifitness.admin.core.system.controller;

import com.inifitness.admin.core.system.model.dto.FileInfo;
import com.inifitness.admin.core.system.service.OssService;
import com.inifitness.common.result.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "07.文件接口")
@RestController
@RequestMapping("/api/v1/files")
@RequiredArgsConstructor
public class FileController {

  private final OssService ossService;

  @PostMapping
  @Operation(summary = "文件上传")
  public Result<FileInfo> uploadFile(
      @Parameter(description = "表单文件对象") @RequestParam(value = "file") MultipartFile file
  ) {
    FileInfo fileInfo = ossService.uploadFile(file);
    return Result.success(fileInfo);
  }

  @DeleteMapping
  @Operation(summary = "文件删除")
  @SneakyThrows
  public Result<Void> deleteFile(
      @Parameter(description = "文件路径") @RequestParam String filePath
  ) {
    boolean result = ossService.deleteFile(filePath);
    return Result.judge(result);
  }
}
